package com.atguigu.gmall.realtime.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author lzc
 * @Date 2022/4/29 16:28
 */
public class AtguiguUtil {
    public static <T> List<T> toList(Iterable<T> it) {
        ArrayList<T> result = new ArrayList<>();
        it.forEach(result::add);
        return result;
    }
    
    public static List<String> splitByIk(String kw) throws IOException {
        ArrayList<String> words = new ArrayList<>();
        // string  -> Reader
        // 内存流
        StringReader reader = new StringReader(kw);
        IKSegmenter ikSegmenter = new IKSegmenter(reader, true);
        
        Lexeme next = ikSegmenter.next();
        while (next != null) {
            String word = next.getLexemeText();
            words.add(word);
            next = ikSegmenter.next();
        }
        
        
        HashSet<String> set = new HashSet<>(words);
        words.clear();
        words.addAll(set);
        
        return words;
    }
    
    
    // currentDate是否等于 lastDate
    public static boolean compareLTZ(String currentDate, String lastData) {
        //2022-05-08 15:30:56.723Z   row_op_ts":"2022-05-08 15:30:56.7Z
        String[] one = currentDate.replaceAll("Z", "").split("\\.");
        String[] two = lastData.replaceAll("Z", "").split("\\.");
        
        String oneDateTime = one[0];
        String twoDateTime = two[0];
        
        int oneMs = Integer.parseInt((one[1] + "000").substring(0, 3));
        int twoMs = Integer.parseInt((two[1] + "000").substring(0, 3));
        
        
        int result = oneDateTime.compareTo(twoDateTime);
        if (result == 0) {
            result = oneMs - twoMs;
        }
        
        return result >= 0;
    }
    
    public static void main(String[] args) {
        System.out.println(compareLTZ("2022-05-08 15:30:56.723Z", "2022-05-08 15:30:56.8Z"));
    }
}
